var ShowTaskData = {};

app.controller('forumSearchCtrl', ['$scope', function ($scope) {
	var needNewPagination = true;
	var searchWords = Utils.getUrlParam("searchWords");
	$(".fm-txt").val(searchWords);
	
	refreshDataList(searchWords, 1, null);
	
	function refreshDataList(searchWords, pageNo, pageOneIds) {
		var param = {
			searchWords: searchWords,
			pageNo: pageNo,
			pageCount: 20,
			pageOneIds: pageOneIds,
		};
		AQ.callAQJSON("searchEntryByWords.do", param, function(result) {
			var list = result.data;
			var data = [];
			for (var i=0; i<list.length; i++) {
				var forumEntry = list[i];
				forumEntry.cateName = DIC.ForumCate(forumEntry.categoryId);
				data.push(forumEntry);
			}
			$scope.$apply(function () {
				$scope.listData = data;
				if(pageNo == 1 && needNewPagination) {	//创建分页
					var num_entries = result.size;
					Utils.newPagination("Pagination", num_entries, function(page_index, jq) {
						if(page_index == 0 && needNewPagination) {
							needNewPagination = false;
							return;
						}
						refreshDataList(searchWords, page_index+1, null);
					}, 20);
				}
			});
		}, function(err, code) {
			Utils.msgDialog(err + "["+ code +"]");
		});
	}
	
	//点击搜索
    $("#search-forum").click(function() {
		var searchWords = $.trim($(".fm-txt").val());
		if(searchWords != "") {
			needNewPagination = true;
			refreshDataList(searchWords, 1, null);
		}
    });
	//点回车搜索
	$(".fm-txt").keydown(function(e) {
        if(e.keyCode == "13") {
			var searchWords = $.trim($(".fm-txt").val());
			if(searchWords != "") {
				needNewPagination = true;
				refreshDataList(searchWords, 1, null);
			}
		}
    });
	
	
}]);
